package org.jzs.mybaseapp.section.home

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.TextView
import android.widget.Toast
import org.jzs.mybaseapp.R
import org.jzs.mybaseapp.common.base.BaseActivity
import org.jzs.mybaseapp.common.retorift.BaseBean
import org.jzs.mybaseapp.common.retorift.DownloadCallBack
import org.jzs.mybaseapp.common.retorift.RequestCallBack
import org.jzs.mybaseapp.common.retorift.RetrofitManager
import org.jzs.mybaseapp.common.utils.AppLog
import org.jzs.mybaseapp.section.home.entity.CourseEntity
import org.jzs.mybaseapp.section.home.entity.DsEntity
import org.jzs.mybaseapp.section.home.entity.LoginBean
import org.jzs.mybaseapp.section.home.entity.LoginEntity
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.util.*
import me.jessyan.progressmanager.body.ProgressInfo
import android.support.annotation.NonNull
import android.widget.Button
import android.widget.ProgressBar
import butterknife.BindView
import butterknife.ButterKnife
import me.jessyan.progressmanager.ProgressListener
import me.jessyan.progressmanager.ProgressManager


/**
 * @author Jzs created 2017/8/2
 * 使用Retrofit请求网络链接的例子
 */
class RetrofitActivity : BaseActivity() {
    private var tv_result: TextView? = null
    private var tv_info: TextView? = null

    var apkUrl = "http://wx.weixinxjj.com/apk/xuejiajia.apk"

    @BindView(R.id.progressBar)
    lateinit var mpb: ProgressBar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        ButterKnife.bind(this)
        tv_result = findViewById(R.id.tv_result) as TextView
        tv_info = findViewById(R.id.tv_info) as TextView
        initListener()
    }

    fun initListener() {
        ProgressManager.getInstance().addResponseListener(apkUrl, getGlideListener())
    }


    //post请求，需要header
    fun login(v: View) {
        HomeApi.getInstance()
                .login("13606088565", "a000000")
                .callBack(object : RequestCallBack {
                    override fun onSuccess(code: Int, msg: String, `object`: Any) {
                        var entity = (`object` as LoginEntity).content
                        tv_result!!.text = entity.toString()
                    }

                    override fun onFailure(code: Int, msg: String, `object`: Any) {

                    }
                }).post()
    }

    //get请求,无参数
    fun kingsoft(v: View) {
        HomeApi.getInstance()
                .dsapi("http://open.iciba.com/dsapi/")
                .callBack(object : RequestCallBack {
                    override fun onSuccess(code: Int, msg: String?, `object`: Any?) {
                        var entity = (`object` as DsEntity)
                        tv_result!!.text = entity.toString()
//                        Toast.makeText(this@HomeActivity, entity.note, Toast.LENGTH_SHORT).show()
                    }

                    override fun onFailure(code: Int, msg: String?, `object`: Any?) {

                    }
                }).postUrl()
    }

    //post请求,以json传递参数
    //http://wx.weixinxjj.com/xjjapi5_4/api.php?f=xjj_course_detail
    //{"goodsId":"1006","shop":"xjj","type":"2","userId":"","token":""}
    fun postJson(v: View) {
        var maps = HashMap<String, String>()
        maps.put("f", "xjj_course_detail")

        var jsonMaps = HashMap<String, String>()
        jsonMaps.put("goodsId", "1006")
        jsonMaps.put("shop", "xjj")
        jsonMaps.put("type", "2")
        jsonMaps.put("userId", "")
        jsonMaps.put("token", "")

        HomeApi.getInstance()
                .courseDetail("http://wx.weixinxjj.com/xjjapi5_4/", "1006")
                .callBack(object : RequestCallBack {
                    override fun onSuccess(code: Int, msg: String?, `object`: Any?) {
                        var entity = (`object` as CourseEntity).data
                        tv_result!!.text = entity.toString()
                    }

                    override fun onFailure(code: Int, msg: String?, `object`: Any?) {

                    }

                }).postJsonUrl()
    }


    //retrofit通常写法
    fun retrofit(v: View) {
        RetrofitManager.getInstance()
                .createReq(LoginApi::class.java)
                .login("13606088565", "a000000")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<BaseBean<LoginBean>> {
                    override fun onCompleted() {

                    }

                    override fun onError(e: Throwable) {
                        AppLog.e("errorMsg=" + e.message)
                    }

                    override fun onNext(entity: BaseBean<LoginBean>) {
                        tv_result!!.text = entity.toString()
                    }
                })
    }

    fun down(v: View) {
        RetrofitManager.getInstance().createBaseApi().download(apkUrl, object : DownloadCallBack() {

            override fun onStart() {
                super.onStart()
                Toast.makeText(this@RetrofitActivity, "xuejiajia.apk" + "  is  start", Toast.LENGTH_SHORT).show()
            }

            override fun onError(e: Throwable) {

            }

            override fun onProgress(fileSizeDownloaded: Long) {
                super.onProgress(fileSizeDownloaded)
                tv_result!!.text = fileSizeDownloaded.toString()
            }

            override fun onSucess(path: String, name: String, fileSize: Long) {
                tv_info!!.text = "path:" + path + ",name:" + name + ",fileSize" + fileSize
            }
        })
    }


    //加载进度监听
    private fun getGlideListener(): ProgressListener {
        return object : ProgressListener {
            override fun onProgress(progressInfo: ProgressInfo) {
                val progress = progressInfo.percent
                mpb.setProgress(progress)
                tv_result!!.setText(progress.toString() + "%")
                if (progressInfo.isFinish) {
                    //说明已经加载完成
                    Toast.makeText(this@RetrofitActivity, "加载完成", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onError(id: Long, e: Exception) {

            }
        }
    }

}

